﻿Public Class frmMultiBarGauge
    Private _MoveControl As PolyMonControls.MoveControl
    Private _SizeControl As PolyMonControls.SizeControl


    Private Sub frmMultiBarGauge_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _MoveControl = New PolyMonControls.MoveControl(pnlGaugeContainer)
        _SizeControl = New PolyMonControls.SizeControl(pnlGaugeContainer)

        Timer1.Enabled = False
        Timer1.Interval = 300 'ms

        'Set up data sources for combo boxes
        cboFillGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.MultiBarGauge.BackgroundType.FillStyles))
        cboBarLabelPosition.DataSource = System.Enum.GetValues(GetType(PolyMonControls.MultiBarGauge.BarLabelPositions))
        cboValueLabelPosition.DataSource = System.Enum.GetValues(GetType(PolyMonControls.MultiBarGauge.ValueLabelStyleType.BarValuePositions))

        'Bind Form objects to Gauge Properties
        cboFillGradientStyle.DataBindings.Add("SelectedItem", MultiBarGauge1.Background, "FillGradientStyle")
        cboFillStyle.DataBindings.Add("SelectedItem", MultiBarGauge1.Background, "FillStyle")
        cboBarLabelPosition.DataBindings.Add("SelectedItem", MultiBarGauge1, "BarLabelPosition")
        cboValueLabelPosition.DataBindings.Add("SelectedItem", MultiBarGauge1.ValueLabelStyle, "Position")

        pbBackColor1.DataBindings.Add("BackColor", MultiBarGauge1.Background, "BackColor1")
        pbBarBackColor.DataBindings.Add("BackColor", MultiBarGauge1, "BarBackColor")
        pbBackColor2.DataBindings.Add("BackColor", MultiBarGauge1.Background, "BackColor2")
        pbBarLabelForeColor.DataBindings.Add("BackColor", MultiBarGauge1, "ForeColor")
        pbValueLabelFontColor.DataBindings.Add("BackColor", MultiBarGauge1.ValueLabelStyle, "FontColor")

        pgBarLabelFont.DataBindings.Add("SelectedObject", MultiBarGauge1, "Font")
        pgValueLabelFont.DataBindings.Add("SelectedObject", MultiBarGauge1.ValueLabelStyle, "Font")

        'Add handlers for color selections
        AddHandler pbBackColor1.Click, AddressOf ChooseColor
        AddHandler pbBackColor2.Click, AddressOf ChooseColor
        AddHandler pbBarBackColor.Click, AddressOf ChooseColor
        AddHandler pbBarLabelForeColor.Click, AddressOf ChooseColor
        AddHandler pbValueLabelFontColor.Click, AddressOf ChooseColor

        'Add Handler for Form object property changes
        AddHandler cboFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboFillGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboBarLabelPosition.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboValueLabelPosition.SelectedIndexChanged, AddressOf ControlValueChanged

        AddHandler pbBackColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor2.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBarBackColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBarLabelForeColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbValueLabelFontColor.BackColorChanged, AddressOf ControlValueChanged

        AddHandler pgBarLabelFont.PropertyValueChanged, AddressOf ControlValueChanged
        AddHandler pgValueLabelFont.PropertyValueChanged, AddressOf ControlValueChanged
    End Sub

    Private Sub MultiBarGauge1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MultiBarGauge1.Click
        _MoveControl.BoundControl = MultiBarGauge1
        _SizeControl.BoundControl = MultiBarGauge1
    End Sub

    Private Sub pnlGaugeContainer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlGaugeContainer.Click
        _MoveControl.BoundControl = Nothing
        _SizeControl.BoundControl = Nothing
    End Sub

    Private Sub btnAnimateBars_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnimateBars.Click
        ToggleBarAnimation()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim rndObj As New Random(New Random().Next(1, 1000))
        rndObj.Next()


        MultiBarGauge1.BarValues(0) = rndObj.Next(0, 100)
        MultiBarGauge1.BarValues(1) = rndObj.Next(0, 100)
        MultiBarGauge1.BarValues(2) = rndObj.Next(0, 100)
        MultiBarGauge1.BarValues(3) = rndObj.Next(0, 100)
        MultiBarGauge1.BarValues(4) = rndObj.Next(0, 100)
        MultiBarGauge1.RefreshGauge()

        'OR, using alternate syntax
        'MultiBarGauge1.BarValues = New Double() {rndObj.Next(0, 100), rndObj.Next(0, 100), rndObj.Next(0, 100), rndObj.Next(0, 100), rndObj.Next(0, 100)}
        'No RefreshGauge call required in this case
    End Sub

    Private Sub ToggleBarAnimation()
        Timer1.Enabled = Not (Timer1.Enabled)
        btnAnimateBars.Text = IIf(Timer1.Enabled, "Stop Animation", "Start Animation").ToString()
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub


    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

    Private Sub MultiBarGauge1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MultiBarGauge1.LostFocus
        _MoveControl.BoundControl = Nothing
        _SizeControl.BoundControl = Nothing
    End Sub
End Class